package cn.com.woxitech.dev.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import cn.com.woxitech.dev.dao.gen.WmLecturePageMapper;
import cn.com.woxitech.dev.model.gen.WmLecturePage;
import cn.com.woxitech.dev.model.gen.WmLecturePageExample;

@Service
public class LecturePageService {
	@Autowired
	WmLecturePageMapper lecturePageMapper;
	
	public List<WmLecturePage> getLecturePageByLectureUuid(String lectureuuid) {
		WmLecturePageExample example  = new WmLecturePageExample();
		example.createCriteria().andLectureidEqualTo(lectureuuid);
		example.setOrderByClause("num, id");
		return lecturePageMapper.selectByExample(example);
	}
	
	public WmLecturePage selectByPrimaryKey(int id) {
		return lecturePageMapper.selectByPrimaryKey(id);
	}
	
	public void updateByPrimaryKey(WmLecturePage lecturePage) {
		lecturePageMapper.updateByPrimaryKeySelective(lecturePage);
	}
	public void updateByPrimaryKeyHard(WmLecturePage lecturePage) {
		// TODO Auto-generated method stub
		lecturePageMapper.updateByPrimaryKey(lecturePage);
	}
	
	public void insertLecturePage(WmLecturePage lecturePage) {
		lecturePageMapper.insertSelective(lecturePage);
	}
	
	public void deleteByPrimaryKey(int id) {
		lecturePageMapper.deleteByPrimaryKey(id);
	}
	public int getPages(String lectureid) {
		WmLecturePageExample example = new WmLecturePageExample();
		example.createCriteria().andLectureidEqualTo(lectureid);
		return lecturePageMapper.countByExample(example);
	}

	public String getFirstLecturePageUrlByLectureUuid(String uuid) {
		// TODO Auto-generated method stub
		WmLecturePageExample example  = new WmLecturePageExample();
		example.createCriteria().andLectureidEqualTo(uuid);
		example.setOrderByClause("num, id limit 0,1");
		List<WmLecturePage> list = lecturePageMapper.selectByExample(example);
		if(list == null || list.isEmpty()){
			return null;
		}else{
			if(list.get(0).getType().equals("img")){
				return list.get(0).getPageurl();				
			}else{
				return list.get(0).getThumburl();
			}

		}
	}

	public void deleteByLectureUuid(String uuid) {
		// TODO Auto-generated method stub
		WmLecturePageExample example  = new WmLecturePageExample();
		example.createCriteria().andLectureidEqualTo(uuid);
		this.lecturePageMapper.deleteByExample(example);
	}

	public WmLecturePage getFirstLecturePageByLectureUuid(String uuid) {
		// TODO Auto-generated method stub
		
		WmLecturePageExample example  = new WmLecturePageExample();
		example.createCriteria().andLectureidEqualTo(uuid);
		example.setOrderByClause("num");
		List<WmLecturePage> l = this.lecturePageMapper.selectByExample(example);
		if(l != null && !l.isEmpty()){
			return l.get(0);
		}else
			return null;
	}

}
